//
//  ReleaseImageViewController.m
//  decorate
//
//  Created by bianqiujin on 14-5-25.
//  Copyright (c) 2014年 bang. All rights reserved.
//

#import "CTAssetsPickerController.h"
#import "ReleaseImageViewController.h"
#import "ASIFormDataRequest.h"

@interface ReleaseImageViewController () <UINavigationControllerDelegate, CTAssetsPickerControllerDelegate>

@property (nonatomic, strong) NSMutableArray *assets;

@end

@implementation ReleaseImageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
//    [self.tableView beginUpdates];
//    [self.tableView insertRowsAtIndexPaths:[self indexPathOfNewlyAddedAssets:assets]
//                          withRowAnimation:UITableViewRowAnimationBottom];
//    
//    [self.assets addObjectsFromArray:assets];
//    [self.tableView endUpdates];
    
    if (!self.assets) {
        self.assets = [[NSMutableArray alloc] init];
    }
    
    [self.assets removeAllObjects];
    
    for (NSInteger i = 0; i < 3; i++) {
        [self.assets addObject:[NSNull null]];
    }
    
    for (NSInteger i = 0; i < assets.count; i++) {
        [self.assets replaceObjectAtIndex:i withObject:[assets objectAtIndex:i]];
    }
    
//    [self.assets addObjectsFromArray:assets];
   
    ALAsset *asset = [self.assets objectAtIndex:0];
    if ((NSNull *)asset != [NSNull null]) {
        self.oneImage.image = [UIImage imageWithCGImage:asset.aspectRatioThumbnail];
    }

    ALAsset *asset2 = [self.assets objectAtIndex:1];
    if ((NSNull *)asset2 != [NSNull null]) {
        self.twoImage.image = [UIImage imageWithCGImage:asset2.aspectRatioThumbnail];
    }

    ALAsset *asset3 = [self.assets objectAtIndex:2];
    if ((NSNull *)asset3 != [NSNull null]) {
        self.threeImage.image = [UIImage imageWithCGImage:asset3.aspectRatioThumbnail];
    }

   
  //    self.bigImage.image = [UIImage imageWithCGImage:[asset.defaultRepresentation fullScreenImage]];
}

//- (NSArray *)indexPathOfNewlyAddedAssets:(NSArray *)assets
//{
//    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    
//    for (int i = self.assets.count; i < self.assets.count + assets.count ; i++)
//        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    
//    return indexPaths;
//    }

- (IBAction)addImages:(id)sender {
    
//    if (!self.assets) {
//        self.assets = [[NSMutableArray alloc] init];
//    }
    
    CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
    picker.maximumNumberOfSelection = 3;
    picker.assetsFilter = [ALAssetsFilter allAssets];
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)uploadImage:(id)sender {
    
    NSData *data = UIImageJPEGRepresentation(self.oneImage.image, 1);

    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://bang.liba.net:8080/image/upload"]];
    
    [request setDelegate:self];
    [request setPostValue:[data base64EncodedStringWithOptions:0] forKey:@"imageByte"];
    [request setRequestMethod:@"POST"];
    
    [request startAsynchronous];
    
    NSError *error = [request error];
    
    if (!error) {
        NSLog(@"success");
    } else {
        NSLog(@"fail");
    }
    
}
@end
